package cn.com.controller.frontController;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.com.Enum.UserAuth;
import cn.com.annotation.Auth;
import cn.com.constant.Code;
import cn.com.db.model.KeyWordSearch;
import cn.com.db.model.Store;
import cn.com.db.service.KeyWordSearchService;
import cn.com.db.service.StoreService;
import cn.com.util.DataResult;
import cn.com.util.JsonUtil;
import cn.com.util.UidUtil;

@Auth(UserAuth.USER)
@RestController
@RequestMapping("/front/keyword")
public class KeyWordSearchController {
	
	@Autowired
	KeyWordSearchService keyWordSearchService;
	@Autowired
	StoreService storeService;
	
	@RequestMapping(method = RequestMethod.GET)
	public String keyword(){
		DataResult dataResult = new DataResult(); 
		List<KeyWordSearch> keywords = keyWordSearchService.getKeyWords();
		dataResult.setCode(Code.SUCCESS);
		dataResult.setData(keywords);
		dataResult.setTotal(keywords.size());
		dataResult.setMessage("获取成功");
		return JsonUtil.toJsonString(dataResult);
	}
	
	@RequestMapping("/{id}")
	public String getStore(@PathVariable("id") String keyWordId){
		String stores = keyWordSearchService.getStoresById(keyWordId);
		List<String> storesIdList = UidUtil.splitStringByLine(stores);
		List<Store> storeList = storeService.getStoreByIdList(storesIdList);
		storeService.modifyEchoTime(storeList);
		DataResult dataResult = new DataResult();
		dataResult.setCode(Code.SUCCESS);
		dataResult.setData(storeList);
		dataResult.setTotal(storeList.size());
		dataResult.setMessage("获取成功");
		return JsonUtil.toJsonString(dataResult);
	}
	
}
